package com.fz.song.springsecurydemo.handler;

import cn.hutool.json.JSONUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Collection;

@Data
@AllArgsConstructor
@Slf4j
public class MySuccessHandler implements AuthenticationSuccessHandler {


    private String url;

    @Override
    public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {

        //authentication中都有写什么数据
        User user = (User) authentication.getPrincipal();
        String username = user.getUsername();
        String password = user.getPassword();
        Collection<GrantedAuthority> authorities = user.getAuthorities();
        System.out.println("username = " + username);
        System.out.println("password = " + password);
        System.out.println("authorities = " + authorities);
        httpServletResponse.sendRedirect(url);
    }
}
